package com.example.ceramics.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.ceramics.R;
import com.example.ceramics.data.Cart;
import com.example.ceramics.data.Order;
import com.example.ceramics.database.dao.CartDaoImpl;
import com.example.ceramics.database.dao.ICartDao;
import com.example.ceramics.database.dao.IOrderDao;
import com.example.ceramics.database.dao.OrderDaoImpl;
import com.example.ceramics.entity.GoodsEntity;
import com.example.ceramics.fragment.CartFragment;
import com.example.ceramics.util.StringUtils;

public class DetailActivity extends BaseActivity {

    private GoodsEntity goodsEntity;
    private TextView tvTitle;
    private TextView tvName;
    private TextView tvPrice;
    private TextView tvSignature;
    private TextView tvType;
    private ImageView ivImg;
    private ImageView ivReturn;
    private Button btnCancel;
    private Button btnBuy;
    private LinearLayout addCart;

    private ICartDao cartDao;
    private IOrderDao orderDao;

    @Override
    protected int initLayout() {
        return R.layout.activity_detail;
    }

    @Override
    protected void initView() {
        tvTitle = findViewById(R.id.tv_top_title);
        tvName = findViewById(R.id.tv_name);
        tvPrice = findViewById(R.id.tv_price);
        tvSignature = findViewById(R.id.tv_signature);
        tvType = findViewById(R.id.tv_type);
        ivImg = findViewById(R.id.iv_img);
        ivReturn = findViewById(R.id.iv_return);
        btnCancel = findViewById(R.id.btn_cancel);
        btnBuy = findViewById(R.id.btn_buy);
        addCart = findViewById(R.id.addCart);
    }

    @Override
    protected void initData() {
        cartDao = new CartDaoImpl(this);
        orderDao = new OrderDaoImpl(mContext);

        showDetail();
        ivReturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        addCart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addToCart();
            }
        });
        btnBuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                buy();
            }
        });
    }

    private void showDetail() {
        goodsEntity = (GoodsEntity) getIntent().getSerializableExtra("detail");
        tvTitle.setText(goodsEntity.getGoodsName());
        tvName.setText(goodsEntity.getGoodsName());
        tvPrice.setText("¥" + goodsEntity.getGoodsPrice());
        tvSignature.setText(goodsEntity.getGoodsSignature());
        tvType.setText(goodsEntity.getGoodsType());
        ivImg.setImageResource(goodsEntity.getGoodsImg());
    }

    private void addToCart() {
        if(StringUtils.isEmpty(getStringFromSp("account"))){
            showToast("请先登录！");
            navigateTo(LoginActivity.class);
            return;
        }
        cartDao.addCart(goodsEntity.getGoodsId(),getStringFromSp("account"),goodsEntity.getGoodsPrice());
        CartFragment.switch_layout();
        showToast("添加成功！");

//        GoodsEntity goodsEntity = (GoodsEntity) this.goodsEntity.clone();
//        Cart.addGoodsEntity(goodsEntity);
//        CartFragment.switch_layout();
//        showToast("添加成功！");
    }

    private void buy() {
        if(StringUtils.isEmpty(getStringFromSp("account"))){
            showToast("请先登录！");
            navigateTo(LoginActivity.class);
            return;
        }
        orderDao.addOrder(goodsEntity.getGoodsId(),getStringFromSp("account"),goodsEntity.getGoodsPrice());
        showToast("购买成功！");

//        GoodsEntity goodsEntity = (GoodsEntity) this.goodsEntity.clone();
//        Order.addGoodsEntity(goodsEntity);
////        OrderActivity.switch_layout();
//        showToast("购买成功！");
    }
}